#memcache for db
#
#
#
from google.appengine.api import memcache
from mode import BlogL1,BlogL2,Config
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template, util

def getCacheBlogL1Page0():
    b1 = memcache.get("BlogL1")
    if b1 is not None:
        return b1
    return RefreshBlogL1Page0()
def RefreshBlogL1Page0():
    strlist = []
    sql = "SELECT * FROM BlogL1 ORDER BY date_time DESC LIMIT 10"
    blogL1_set = db.GqlQuery(sql)
    
    for artical in blogL1_set:
        strlist1=[]
        strlist1.append(artical.key().id())
        strlist1.append(artical.title)
        strlist1.append(artical.tags)
        strlist1.append(artical.intro)
        strlist1.append(artical.date_time.strftime('%Y-%m-%d %H:%M:%S') )
        strlist1.append(str(artical.counter_visit))
        strlist1.append(str(artical.counter_comment))
        strlist.append(strlist1)
    #86400= 3600 * 24 hours
    memcache.set("BlogL1",strlist,86400)
    return strlist 

def getCacheConfig():
    b1 = memcache.get("Config")
    if b1 is not None:
        return b1
    return RefreshCacheConfig()
def RefreshCacheConfig():
    dRet = {}
    dRet['count']= BlogL1.all().count()
    lSidebar = []
    query = Config.all()
    for item in query:
        lSidebar.append(item.content)
    dRet['sidebar'] = lSidebar
    
    memcache.set("Config",dRet)
    
    return dRet 